﻿using System;
using System.Collections.Generic;

using CNVP.Config;

namespace CNVP.JShop.Publish
{
    /// <summary>
    /// 标签中的语句块
    /// </summary>
    public partial class LabelMass
    {
        protected enum EnumLabelType
        {
            Position, Dynamic, SiteNav, NewsList, ReadNews, ProductList, Common, NewsListPage, ProductListPage, SearchListPage, FriendLink, ReadProduct, ProductHot, ProductPic, ProductStandard, SearchPage, ProductHotForNews, ProductExtendProperty, ProductPropertyDescripttion, AdSet, BrankList, ProductHistory
        };
        #region 标签参数
        /// <summary>
        /// Loop次数，Loop次数为0或者unLoop标签，为0
        /// </summary>
        protected int Param_Loop;
        /// <summary>
        /// 当前栏目序号
        /// </summary>
        protected string Param_CurrentColumnID;
        /// <summary>
        /// 频道ID
        /// </summary>
        protected int Param_ChID = 0;
        /// <summary>
        /// 当前的专题ID
        /// </summary>
        protected string Param_CurrentSpecialID;
        /// <summary>
        /// 当前新闻ID
        /// </summary>
        protected string Param_CurrentNewsID;
        /// <summary>
        /// 当前的(频道)栏目ID
        /// </summary>
        protected int Param_CurrentCHClassID;
        /// <summary>
        /// 当前的(频道)专题ID
        /// </summary>
        protected int Param_CurrentCHSpecialID;
        /// <summary>
        /// 当前(频道)新闻ID
        /// </summary>
        protected int Param_CurrentCHNewsID;
        /// <summary>
        /// 标签类型
        /// </summary>
        protected EnumLabelType Param_LabelType;
        #endregion 标签参数
        /// <summary>
        /// 标签格式是否有效
        /// </summary>
        protected bool FormatValid = true;
        /// <summary>
        /// 格式非法的说明
        /// </summary>
        protected string InvalidInfo = string.Empty;
        /// <summary>
        /// 标签的主体部份
        /// </summary>
        protected string Mass_Primary = string.Empty;
        /// <summary>
        /// 标签中间插入代码部份
        /// </summary>
        public string Mass_Inserted = string.Empty;
        /// <summary>
        /// 标签原始内容
        /// </summary>
        protected string Mass_Content = string.Empty;
        /// <summary>
        /// 标签所有参数
        /// </summary>
        protected LabelParameter[] _LblParams = null;
        /// <summary>
        /// 模板类型
        /// </summary>
        protected TempType _TemplateType;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="_MassContent"></param>
        public LabelMass(string _MassContent)
        {
            Mass_Content = _MassContent;
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="_MassContent"></param>
        /// <param name="_CurrentColumnID"></param>
        public LabelMass(string _MassContent, string _CurrentColumnID)
        {
            Mass_Content = _MassContent;
            if (!string.IsNullOrEmpty(_CurrentColumnID))
            {
                Param_CurrentColumnID = _CurrentColumnID;
            }
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="masscontent">标签的内容</param>
        /// <param name="connection">数据库连接</param>
        /// <param name="currentclassid">当前栏目ID，如果不设置，请赋为null</param>
        /// <param name="currentnewsid">当前新闻ID，如果不设置，请赋为null</param>
        public LabelMass(string _MassContent, string _CurrentColumnID,  string _CurrentNewsID)
        {
            Mass_Content = _MassContent;
            if (_CurrentColumnID == string.Empty || _CurrentColumnID == null) { Param_CurrentColumnID = null; }
            else { Param_CurrentColumnID = _CurrentColumnID; }
            Param_CurrentCHNewsID =Convert.ToInt32(_CurrentNewsID);
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="masscontent">标签的内容</param>
        /// <param name="connection">数据库连接</param>
        /// <param name="currentclassid">当前栏目ID，如果不设置，请赋为null</param>
        /// <param name="currentspecialid">当前专题ID，如果不设置，请赋为null</param>
        /// <param name="currentnewsid">当前新闻ID，如果不设置，请赋为null</param>
        public LabelMass(string masscontent, string currentclassid, string currentspecialid, string currentnewsid, int ChID, int currentchclassid, int currentchspecialid, int currentchnewsid)
        {
            //Mass_Content = masscontent;
            //Param_ChID = ChID;
            //if (currentclassid == string.Empty || currentclassid == null) { Param_CurrentClassID = null; }
            //else { Param_CurrentClassID = currentclassid; }
            //if (currentspecialid == string.Empty || currentspecialid == null) { Param_CurrentSpecialID = null; }
            //else { Param_CurrentSpecialID = currentspecialid; }
            //if (currentnewsid == string.Empty || currentnewsid == null) { Param_CurrentNewsID = null; }
            //else { Param_CurrentNewsID = currentnewsid; }
            //Param_CurrentCHClassID = currentchclassid;
            //Param_CurrentCHSpecialID = currentchspecialid;
            //Param_CurrentCHNewsID = currentchnewsid;
        }
        /// <summary>
        /// 解析标签内容
        /// </summary>
        public void ParseContent()
        {
            int pos1 = Mass_Content.IndexOf(']');
            int pos2 = Mass_Content.LastIndexOf('[');
            if (pos2 < 1)
                throw new Exception("标签格式错误");
            Mass_Primary = Mass_Content.Substring(1, pos1 - 1);
            int n = pos2 - pos1 - 1;
            if (n > 0)
                Mass_Inserted = Mass_Content.Substring(pos1 + 1, n);
            ParsePrimary();
        }
        /// <summary>
        /// 分析标签参数，拆分为数组
        /// </summary>
        private void ParsePrimary()
        {
            if (Mass_Primary.IndexOf(",") > 0)
            {
                string[] _mass_p = Mass_Primary.Split(',');
                if (_mass_p[0].Equals("NT:Loop"))
                    Param_Loop = 1;
                else if (_mass_p[0].Equals("NT:UnLoop"))
                    Param_Loop = 0;
                else
                {
                    FormatValid = false;
                    InvalidInfo = "标签内容不是以[NT:UnLoop或[NT:Loop开始";
                }
                int n = _mass_p.Length;
                IList<LabelParameter> l = new List<LabelParameter>();
                l.Clear();
                for (int i = 1; i < n; i++)
                {
                    if (!FormatValid) break;
                    string s = _mass_p[i];
                    int pos = s.IndexOf('=');
                    if (pos < 0)
                        continue;
                    LabelParameter p;
                    p.LPName = s.Substring(0, pos).Trim();
                    p.LPValue = s.Substring(pos + 1).Trim();
                    #region 对标签的必要参数进行一些处理
                    switch (p.LPName)
                    {
                        case "NT:Number":
                            if (Param_Loop.Equals(1))
                            {
                                try
                                {
                                    Param_Loop = int.Parse(p.LPValue);
                                }
                                catch
                                {
                                    FormatValid = false;
                                    InvalidInfo = "NT:Number的值不是有效的数字";
                                }
                            }
                            break;
                        case "NT:LabelType":
                            try
                            {
                                Param_LabelType = (EnumLabelType)System.Enum.Parse(typeof(EnumLabelType), p.LPValue);
                            }
                            catch
                            {
                                FormatValid = false;
                                InvalidInfo = "NT:LabelType指定的类型不存在";
                            }
                            break;
                        case "NT:Root":
                            break;
                        default:
                            AddParameter(p, ref l);
                            break;
                    }
                    #endregion 对标签的必要参数进行一些处理
                }
                int ln = l.Count;
                if (FormatValid && ln > 0)
                {
                    _LblParams = new LabelParameter[ln];
                    l.CopyTo(_LblParams, 0);
                }
            }
        }
        /// <summary>
        /// 解析标签生成HTML
        /// </summary>
        /// <returns></returns>
        public string Parse()
        {
            if (!FormatValid)
                return Mass_Content;
            switch (this.Param_LabelType)
            {
                case EnumLabelType.Dynamic: return this.Analyse_Dynamic();
                case EnumLabelType.Position: return this.Analyse_Position(Convert.ToInt32(Param_CurrentColumnID));
                case EnumLabelType.SiteNav: return this.Analyse_SiteNav(Convert.ToInt32(Param_CurrentColumnID));
                case EnumLabelType.NewsList: return this.Analyse_NewsList(Convert.ToInt32(Param_CurrentColumnID));
                case EnumLabelType.NewsListPage: return this.Analyse_NewsListPage(Convert.ToInt32(Param_CurrentColumnID));
                case EnumLabelType.ReadNews:  return this.Analyse_ReadNews(Convert.ToInt32(Param_CurrentColumnID), Convert.ToInt32(Param_CurrentCHNewsID));
                case EnumLabelType.ProductList: return this.Analyse_ProductList(Convert.ToInt32(Param_CurrentColumnID));
                case EnumLabelType.ProductListPage: return this.Analyse_ProductListPage(Convert.ToInt32(Param_CurrentColumnID));
                case EnumLabelType.Common: return this.Analyse_Common();
                case EnumLabelType.FriendLink: return this.Analyse_FriendLink(Convert.ToInt32(Param_CurrentColumnID));
                case EnumLabelType.ReadProduct: return this.Analyse_ReadProduct(Convert.ToInt32(Param_CurrentColumnID), Convert.ToInt32(Param_CurrentCHNewsID));
                case EnumLabelType.ProductHot: return this.Analyse_ProductHot(Convert.ToInt32(Param_CurrentCHNewsID));
                case EnumLabelType.ProductHistory: return this.Analyse_ProductHistory();
                case EnumLabelType.ProductHotForNews: return this.Analyse_ProductHotForNews(Convert.ToInt32(Param_CurrentCHNewsID));
                case EnumLabelType.ProductPic: return this.Analyse_ProductPic(Convert.ToInt32(Param_CurrentCHNewsID));
                case EnumLabelType.ProductStandard: return this.Analyse_ProductStandard(Convert.ToInt32(Param_CurrentCHNewsID));
                case EnumLabelType.SearchPage: return this.Analyse_SearchPage();
                case EnumLabelType.ProductExtendProperty: return this.Analyse_ProductExtendProperty(Convert.ToInt32(Param_CurrentCHNewsID));
                case EnumLabelType.ProductPropertyDescripttion: return this.Analyse_ProductPropertyDescripttion(Convert.ToInt32(Param_CurrentCHNewsID));
                case EnumLabelType.AdSet: return this.Analyse_AdSet(Convert.ToInt32(Param_CurrentColumnID));
                case EnumLabelType.BrankList: return this.Analyse_BrankList();
                default:
                    return string.Empty;
            }
        }

        /// <summary>
        /// 解析标签生成HTML
        /// </summary>
        /// <returns></returns>
        public string Parse(int ChID)
        {
            if (!FormatValid)
                return Mass_Content;
            switch (this.Param_LabelType)
            {
                //case EnumLabelType.ChannelList: return this.Analyse_ChannellList("", ChID);
                //case EnumLabelType.ChannelClassList: return this.Analyse_ChannelClassList(ChID);
                //case EnumLabelType.ChannelContent: return this.Analyse_ChannelContent(ChID);
                //case EnumLabelType.ChannelSearch: return this.Analyse_ChannelSearch(ChID);
                //case EnumLabelType.ChannelRSS: return this.Analyse_ChannelRSS(ChID);
                //case EnumLabelType.ChannelFlashFilt: return this.Analyse_ChannelFlash(ChID);
                default:
                    return string.Empty;
            }
        }

        /// <summary>
        /// 将一个参数加入参数队列
        /// </summary>
        /// <param name="lp">标签参数</param>
        /// <param name="list">列表</param>
        private void AddParameter(LabelParameter lp, ref IList<LabelParameter> list)
        {
            bool flag = true;
            foreach (LabelParameter p in list)
            {
                if (p.LPName.Equals(lp.LPName))
                {
                    flag = false;
                    break;
                }
            }
            if (flag)
            {
                list.Add(lp);
            }
        }
        /// <summary>
        /// 标签的初始内容
        /// </summary>
        public string Content
        {
            get { return Mass_Content; }
        }
        /// <summary>
        /// 查找某一标签参数的值
        /// </summary>
        /// <param name="ParamName">标签参数的名称</param>
        /// <returns></returns>
        public string GetParamValue(string ParamName)
        {
            string result = string.Empty;
            if (_LblParams == null)
                return null;
            int n = _LblParams.Length;
            for (int i = 0; i < n; i++)
            {
                LabelParameter p = _LblParams[i];
                if (p.LPName.Equals(ParamName))
                {
                    result = p.LPValue;
                    break;
                }
            }
            return result.Equals(string.Empty) ? null : result;
        }
        /// <summary>
        /// 标签内容格式不正确的说明
        /// </summary>
        public string FormatInvalidMsg
        {
            get { return InvalidInfo; }
        }
        /// <summary>
        /// 设置或获取当前的模板类型
        /// </summary>
        public TempType TemplateType
        {
            set { _TemplateType = value; }
            get { return _TemplateType; }
        }
    }
}